//
//  GeoInfo.m
//  GeoData
//
//  Created by zhenhua zhou on 12-7-7.
//  Copyright (c) 2012年 ECUST. All rights reserved.
//
#include "TaxiPriceCalculator.h"
#import "GeoInfo.h"
#import "UrlBuilder.h"
//#import "TaxiPriceCalculator.h"

//#import "SBJson.h"

static GeoInfo* geoInfoInstance = nil;
@implementation GeoInfo


- (GeoInfo *)init
{
    self = [super init];
    if(self)
    {
        mGeoData = [[GeoData alloc] init];
        mTimeData = [[NSDateFormatter alloc] init];
        if(mTimeData)
        {
            [mTimeData setDateStyle:NSDateFormatterShortStyle]; 
            [mTimeData setDateFormat:@"HHmmss"]; 
        }
        
        
   //     mMapView = [[MagiMapView alloc] init];
        
mNetClient = [[HttpClient alloc] init];
    }
    
    return self;
}

-(void)dealloc
{
    if( mGeoData)
    {
        [mGeoData dealloc];
    }
    
    if(mTimeData)
    {
        [mTimeData dealloc];
    }
    if(mNetClient)
    {
        [mNetClient dealloc];
    }
    
[super dealloc];
}

-(int32_t)getCurRouteLength
{
    if(mGeoData==nil)
    {
        return 0;
    }
    
    int count = [mGeoData.CurRouteData.Segments count];
    int result = 0;
    for(int i=0; i<count; i++)
    {
        Segment * seg = [mGeoData.CurRouteData.Segments objectAtIndex:i];
        if(seg)
        {
           
            result += [seg getDistance];
            
        }
    }
    
    return result;
    
}

-(RouteData *) getCurRoute;
{
    return mGeoData.CurRouteData;
}

-(NSString *)getCurCity
{
    return mGeoData.City;
}

-(NSString *) getCurTime
{
    NSString *result=[mTimeData stringFromDate:[NSDate date]]; 
   // long curr=[result longLongValue];
    
    return result;

}

-(void) testFunc:(UITextField *)textField
{
    NSString * url = [[UrlBuilder getInstance] buildDirectionUrlByAddressFrom:@"仙霞路333号" to:@"中山公园"];
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
 //   mTestTextField = textField;
    
    [mNetClient requestWithUrl:url delegate:self];
    
}

- (void) onNetworkReturn:(NSString *) data
{

 //   NSArray *root = (NSArray*)data;
 //   assert([root isKindOfClass:[NSArray class]]);
    
 //   RouteData * routData = [RouteData buildRouteDataFromJson:root];
    [mGeoData resetRouteData];
    [self parserDataWithString:data];
    float distance = [self getCurRouteLength];
    distance /= 1000;
    
    double re = TaxiPriceCalculator::CalculatePrice( distance, TaxiPriceCalculator::Shanghai, 20, 0);
    
//    if(mTestTextField!=nil)
//    {
//        mTestTextField.text =  [NSString stringWithFormat:@"%f",re];
//    }
    
    if(mPriceLabel!=nil)
    {
        mPriceLabel.text = [NSString stringWithFormat:@"%f",re];
    }
    
    if(mDistanceLabel!=nil)
    {
        mDistanceLabel.text = [NSString stringWithFormat:@"%f",distance];
    }
    
    
[self showRoute:mMapView];
}

- (void) parserDataWithString:(NSString *) str
{
    [mGeoData parserDataWithString:str];
}

- (void) searchRouteFrom:(NSString *)start To:(NSString *)end
{
    [mMapView clearRoutes];
    [mGeoData resetRouteData];
    //clear data first.
    
    NSString * url = nil;
    url = [[UrlBuilder getInstance] buildDirectionUrlByAddressFrom:start to:end];
    
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [mNetClient requestWithUrl:url delegate:self];
}

- (void) searchRouteFromPos:(CLLocationCoordinate2D *)start To:(NSString *)end 
{
    
    [mMapView clearRoutes];
    [mGeoData resetRouteData];
    //clear data first.
    
    NSString * url = nil;
    
    GeoPos * pos = [[[GeoPos alloc]init] autorelease];
    pos.Lat = start->latitude;
    pos.Lon = start->longitude;
    url = [[UrlBuilder getInstance] buildDirectionUrlByFromPos:pos toAddress:end];
    
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
  //  mTestTextField = textField;
    
    [mNetClient requestWithUrl:url delegate:self];
}

-(void) setMagiMapView:(MagiMapView *) mapView
{
    mMapView = mapView;
}

-(void) showRoute:(MagiMapView *) mapView
{
    if(mapView)
    {
    [mapView setMapPos:mGeoData.CurRouteData.MinPos 
                armong:mGeoData.CurRouteData.MinPos 
                   at:mGeoData.CurRouteData.MaxPos];
  //  [mapView drawRoute:mGeoData.CurRouteData ];
        [mapView drawRoute:mGeoData.CurRouteData];
    }
}

-(void) setPriceLabel:(UILabel *)label
{
    mPriceLabel = label;
}

-(void) setDistanceLabel:(UILabel *)label
{
    mDistanceLabel = label;
}

-(void) setDurationLabel:(UILabel *)label
{
    mDurationLabel = label;
}


+(GeoInfo*) getInstance
{
    if(geoInfoInstance==nil)
    {
        geoInfoInstance = [[GeoInfo alloc] init];
    }
return geoInfoInstance;
}


@end
